<template>
    <div class="index">
        <div class="top-banner">
            <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
                <van-swipe-item v-for="item in banners" :key="item.id">
                    <img :src="item.picUrl" alt="banner">
                </van-swipe-item>
            </van-swipe>
            <use-set></use-set>
        </div>
        <div class="type-box" @click="$router.push('/barginList')">
            <type-title>全民砍价</type-title>
            <bargin-card :bargin="obj.detail"></bargin-card>
        </div>
        <div class="type-box">
            <type-title @click="$router.push('/special')">精选专题</type-title>
            <div class="news-list">
                <ul>
                    <li v-for="item in obj.news" :key="item.id">
                        <img :src="item.pic" alt="">
                        <h3 v-html="item.title"></h3>
                        <p class="ellipsis">{{item.descript}}</p>
                    </li>
                </ul>
            </div>
        </div>
        <div class="type-box">
            <type-title @click="$router.push('/recomendList')">人气推荐</type-title>
            <div class="recoms-box">
                <recomend-card v-for="(item, i) in obj.recoms" :key="item.id" v-show="i > 8 && i < 13" :good="item"></recomend-card>
            </div>
        </div>
    </div>
</template>
<script>
import { getBanners, getBargins, getNews, getRecos } from '@/api' 
import { ref, reactive } from 'vue'
import useSet from '@/components/useSet/useSet'
import typeTitle from '@/components/typeTitle/typeTitle'
import barginCard from '@/components/barginCard/barginCard'
import recomendCard from '@/components/recomendCard/recomendCard'
// ref是vue3声明响应式数据的方法 但是他不能用来声明对象
// ref可以用来声明基本数据类型和数组 并且声明的数据具有相应式
// reactive 可以用来声明对象类型的响应式数据 并且声明的数据中的每一个属性都是响应式的
export default {
    components: {
        useSet,
        typeTitle,
        barginCard,
        recomendCard
    },
    setup() {
        const banners = ref([])

        const obj = reactive({
            detail: {},
            news: [],
            recoms: []
        })
        // 请求轮播图数据
        getBanners().then(res => {
            banners.value = res
        })
        // 请求砍价列表数据
        getBargins().then(res => {
            // console.log(res);
            obj.detail = res.goodsMap['99764']
        })
        // 请求精选专题列表数据
        getNews().then(res =>{
            // console.log(res);
            obj.news = res
        })

        // 请求人气推荐列表数据
        getRecos().then(res => {
            // console.log(res);
            obj.recoms = res
        })

        return {
            banners,
            obj
        }
    }
}
</script>
<style lang="scss">
    .index {
        padding-bottom: 20px;
    }
    .top-banner {
        width: 100%;
        position: relative;
        background-color: #fff;
        img {
            width: 100%;
            height: auto;
        }
    }
    .type-box {
        margin-top: 30px;
        background-color: #fff;
    }
    .news-list {
        width: 100%;
        background-color: #fff;
        overflow: auto;
        padding-bottom: 20px;
        ul {
            display: flex;
            li {
                width: 636px;
                flex-shrink: 0;
                margin-left: 30px;
                img {
                    width: 100%;
                    height: 412px;
                    border-radius: 16px;
                }
                h3 {
                    font-size: 30px;
                    margin: 20px 0;
                }

                p {
                    width: 600px;
                    font-size: 24px;
                    color: #ccc;
                }
            }
        }
    }
    
</style>